/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.serviportales.serviquality.managers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.serviportales.serviportales.model.GenericTreeModel;
import com.serviportales.serviquality.dao.PlantillaNodoDAO;
import com.serviportales.serviquality.exception.ServiqualityException;
import com.serviportales.serviquality.model.Plantilla;
import com.serviportales.serviquality.model.PlantillaNodo;

@Service
@Transactional(rollbackFor = { ServiqualityException.class })
public class MgrPlantillaNodo {


	@Autowired
	private PlantillaNodoDAO plantillaNodoDAO;
	@Autowired
	private MessageSource messageSource;

	public void grabarPlantillaNodo(PlantillaNodo plantillaNodo) throws ServiqualityException {
		try {
			PlantillaNodo plantillaNodoFilter = plantillaNodoDAO.selectByID(plantillaNodo.getPlantillaNodoPK());
			if (plantillaNodoFilter != null) {
				plantillaNodoDAO.update(plantillaNodoFilter);
			} else {
				plantillaNodoDAO.insert(plantillaNodo);
			}
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.GRABAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public void eliminarPlantillaNodo(PlantillaNodo plantillaNodo) throws ServiqualityException {
		try {
			plantillaNodoDAO.delete(plantillaNodo);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.ELIMINAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public List<PlantillaNodo> buscarChildren(PlantillaNodo plantillaNodo) throws ServiqualityException {
		try {
			
			return plantillaNodoDAO.selectChildren(plantillaNodo);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.BUSCAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public GenericTreeModel<PlantillaNodo> createTree(Plantilla plantilla) throws ServiqualityException {
		try {
			GenericTreeModel<PlantillaNodo> tree = new GenericTreeModel<PlantillaNodo>();
			PlantillaNodo plantillaNodoRoot = new PlantillaNodo();
			plantillaNodoRoot.getPlantillaNodoPK().setPlantilla(plantilla);
			tree.setRootNode(plantillaNodoRoot.getPlantillaNodoPK().getPlantilla().getNombre(), plantillaNodoRoot);

			List<PlantillaNodo> nodos = plantillaNodoDAO.selectCreateTree(plantilla);
			for (PlantillaNodo plantillaNodo : nodos) {
				tree.addNode(plantillaNodo.getParent(), plantillaNodo.getPlantillaNodoPK().getNodo().getNombre(), plantillaNodo);
			}

			tree.finishConfig();
			return tree;
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.BUSCAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}
}
